﻿using System;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.SVN.Tasks;
using NAnt.SVN.Util;
using NAnt.SVN.Tasks.Constants;

namespace NAnt.SVN.Attributes
{
    /// <summary>
    /// Validator for SVN Keywords
    /// $Id$
    /// </summary>
    public sealed class EOLStyleValidator : ValidatorAttribute
    {
        private SVNEolStyle eolstyleobj = new SVNEolStyle();
        
        #region Public Instance Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="KeywordsValidator" /> 
        /// class.
        /// </summary>
        public EOLStyleValidator()
        {
        }

        #endregion Public Instance Constructors

        #region Override implementation of ValidatorAttribute

        /// <summary>
        /// Checks if the specified value can be converted to keywords
        /// </summary>
        /// <param name="value">The value to be checked.</param>
        /// <exception cref="ValidationException"><paramref name="value" /> cannot be converted to a <see cref="bool" />.</exception>
        public override void Validate(object value)
        {
            try
            {
                string stringvalue = Convert.ToString(value);
                string[] values = stringvalue.Split(',');
                foreach (string v in values)
                {
                    if (!eolstyleobj.IsStaticField(v))
                        throw new Exception(string.Format(Strings.GetString("SVN0309"), value));
                }                
            }
            catch (Exception ex)
            {
                throw new ValidationException(string.Format(Strings.GetString("SVN0301"), value.ToString()), ex);
            }
        }

        #endregion Override implementation of ValidatorAttribute
    }
}
